package com.single.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.Equipment;
import com.data.dao.ChargeDao;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeMin;
import com.data.entity.ChargeMonth;
import com.data.entity.ChargeWeek;
import com.single.dao.SingleDao;

@Service("singleService")
public class SingleServiceImpl implements SingleService{
	@Resource(name="equipmentDao")
	private  EquipmentDao equipmentDao;
	@Resource(name = "lineConfigDao")
	private LineConfigDao lineConfigDao;
	
	@Resource(name="chargeDao")
	private ChargeDao chargeDao;
	@Resource(name="singleDao")
	private SingleDao singleDao;
	/**
	 * 单机电度电费日统计
	 * @param equId
	 * @param model
	 */
	@Override
	public void singleChargesByDay(int equId,String belongDate, Map<String, Object> model) {
		List<String> xTimes=Arrays.asList(new String[]{
				"00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00",
				"06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", 
				"12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00",
				"18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30"});
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> belongTimes=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			List<ChargeMin> list=chargeDao.getChargeMinsByDay(equ.getId(), belongDate, "N");
			if(list.size()!=0){
				for(ChargeMin cm:list){
					DateTime dt = new DateTime(cm.getBelongTime());
					String datetime = dt.toString("HH:mm");
					belongTimes.add(datetime);
					totalCharges+=cm.getCharge();
					totalRelativeComps+=cm.getRelativeComp();
					charges.add(cm.getCharge());
					relativeComps.add(cm.getRelativeComp());
				}
			}
		}
		
		if(belongTimes.size()<47){
			for(String xt:xTimes){
				if(!belongTimes.contains(xt)){
					int index=xTimes.indexOf(xt);
					belongTimes.add(index, xt);
					charges.add(index, 0.0);
					relativeComps.add(index, 0.0);
				}
			}
		}
		model.put("belongTimes", belongTimes);
		model.put("charges", charges);
		model.put("equ", equ);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
	/**
	 * 单机电度电费日综合统计
	 * @param equId
	 * @param startDate
	 * @param endDate
	 * @param model
	 */
	@Override
	public void singleChargesByDate(int equId, String startDate,String endDate, Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> belongDate=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			List<ChargeDay> list=singleDao.getChargeDayByDate(equ.getId(), startDate, endDate, "N");
			if(list.size()!=0){
				for(int i=0;i<list.size();i++){
					ChargeDay cd=list.get(i);
					totalCharges+=cd.getCharge();totalRelativeComps+=cd.getRelativeComp();
					DateTime dt = new DateTime(cd.getBelongDate());
					belongDate.add(dt.toString("yyyy-MM-dd"));
					charges.add(cd.getCharge());
					relativeComps.add(cd.getRelativeComp());
				}
			}
		}
		model.put("belongDate", belongDate);
		model.put("charges", charges);
		model.put("equ", equ);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
	/**
	 * 单机电度电费周统计
	 * @param equId
	 * @param belongYear
	 * @param startWeek
	 * @param endweek
	 * @param model
	 */
	@Override
	public void singleChargesByWeek(int equId, int belongYear, int startWeek,int endWeek, Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> belongWeek=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			List<ChargeWeek> list=singleDao.getChargeWeekByWeek(equ.getId(), belongYear, startWeek, endWeek, "N");
			if(list.size()!=0){
				for(int i=0;i<list.size();i++){
					ChargeWeek cd=list.get(i);
					totalCharges+=cd.getCharge();totalRelativeComps+=cd.getRelativeComp();
					belongWeek.add(cd.getBelongWeek()+"周");
					charges.add(cd.getCharge());
					relativeComps.add(cd.getRelativeComp());
				}
			}
		}
		model.put("belongWeek", belongWeek);
		model.put("charges", charges);
		model.put("equ", equ);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
	/**
	 * 单机电度电费月统计
	 * @param equId
	 * @param belongMonth
	 * @param model
	 */
	@Override
	public void singleChargesByMonth(int equId, String startMonth,String endMonth,Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> belongMonths=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			List<ChargeMonth> list=singleDao.getChargeMonthByMonth(equ.getId(),startMonth, endMonth, "N");
			if(list.size()!=0){
				for(int i=0;i<list.size();i++){
					ChargeMonth cd=list.get(i);
					totalCharges+=cd.getCharge();totalRelativeComps+=cd.getRelativeComp();
					DateTime dt = new DateTime(cd.getBelongDate());
					belongMonths.add(dt.getMonthOfYear()+"月");
					charges.add(cd.getCharge());
					relativeComps.add(cd.getRelativeComp());
				}
			}
		}
		model.put("belongMonths", belongMonths);
		model.put("charges", charges);
		model.put("equ", equ);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
		
	}
}
